﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using XCI.Windows.Helper;
using XCI.Core;

namespace XCI.Windows.Controls
{
    //[System.ComponentModel.DesignerCategory("Code")]
    public partial class BaseXtraUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        public BaseXtraUserControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (DesignMode) WinHelper.DesignInit();
        }

        /// <summary>
        /// 是否正在初始化
        /// </summary>
        protected bool IsInit { get; private set; }

        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="action">初始化函数</param>
        protected void BeginInit(Action action)
        {
            try
            {
                IsInit = true;
                action?.Invoke();
            }
            finally
            {
                IsInit = false;
            }
        }

        /// <summary>
        /// 控件初始化完成
        /// </summary>
        /// <param name="action">调用函数</param>
        protected void EndInit(Action action)
        {
            if (IsInit == false)
            {
                action?.Invoke();
            }
        }

        /// <summary>
        /// 开始计算程序执行时间(默认返回:查询耗时：xx毫秒)
        /// </summary>
        /// <param name="action">执行内容</param>
        /// <returns>返回程序执行的时间</returns>
        protected string ElapsedTime(Action action)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            action();
            stopwatch.Stop();
            var ts = stopwatch.Elapsed;
            return $"耗时:{ts.TotalMilliseconds:###0.##} ms";
        }

        /// <summary>
        /// 开始计算程序执行时间(默认返回:查询耗时：xx毫秒)
        /// </summary>
        /// <param name="barItem">文本框项</param>
        /// <param name="action">执行内容</param>
        /// <returns>返回程序执行的时间</returns>
        protected void ElapsedTime(BarStaticItem barItem, Action action)
        {
            barItem.Caption = ElapsedTime(action);
        }

        protected void EnterHandle(KeyPressEventArgs e, Action action)
        {
            if (e.KeyChar == 13) //回车
            {
                action?.Invoke();
                e.Handled = true;
            }
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="data">导出的数据</param>
        /// <param name="sheetName">Sheet名称</param>
        /// <param name="heads">表头列表</param>
        /// <param name="defaultFileName">默认文件名</param>
        protected void ExcelExport<T>(IEnumerable<T> data, string sheetName,
                                      List<ExcelHead> heads, string defaultFileName = null) where T : class, new()
        {
            WinHelper.ExcelExportCore(this,() => ExcelHelper.Export(data, sheetName, heads), defaultFileName);
        }

        /// <summary>
        /// 显示导出文件对话框
        /// </summary>
        /// <param name="func"></param>
        /// <param name="defaultFileName"></param>
        protected void ExcelExportCore(Func<byte[]> func, string defaultFileName = null)
        {
            WinHelper.ExcelExportCore(this,func, defaultFileName);
        }

        protected void ControlInvoke(Control control, Action action)
        {
            control.Invoke(new Action(action));
        }
    }
}
